<?php
namespace QnuiRings\Controller;
use Think\Controller;
// 软件名称：微折促销管理信息系统
// 代码日期：v3.0 2017-8-31
// 作者：杨道泉 
// 文件名：MPageController.class.php 
// 功能： 读取、创建手机页面

class PageModelController extends QnuiBaseController {
	public function get_shop_models(){
		
		$page  = I("page");
		$page_size  = I("page_size");
		$PG = D("ShopModelView");
		$where["PageModelsInshop.status"]="normal";
		$total = $PG->where($where)->count();
		$models = $PG->where($where)->limit(($page-1)*$page_size,$page_size)->getField("id,name,preview,shop_model_id");

		$resp = new \stdClass();
		if($models===null){
			$models = Array();
		}
		$resp->data=array_values($models);
		$resp->total=(int)$total;
		$this->ajaxReturn(pre_resp($resp));
	}
	public function get_models(){
		$page  = I("page");
		$page_size  = I("page_size");
		$tags  = I("tags");
		$version  = I("version");
		foreach($tags as $key=>$tag_ty){
			if($key=="tag_price"){
				foreach($tag_ty as $tag){
					switch($version){
						case "FW_GOODS-1840332-1":
							$field = "price_v1";
							break;
						case "FW_GOODS-1840332-v2":
							$field = "price_v2";
							break;
						case "FW_GOODS-1840332-v3":
						case "FW_GOODS-1840332-v4":
							$field = "price_v3";
							break;
					}
					
					$where[$field][] = Array("ELT",$tag);
				}
				$where[$field][]="or";
			}else{
				foreach($tag_ty as $tag){
					$where[$key][] = Array("eq",$tag); 
				}
				$where[$key][]="or";
			}
		}
		$PG = D("PageModelView");
		$where["PageModels.status"]="normal";
		$total = $PG->where($where)->count();
		$models = $PG->where($where)->limit(($page-1)*$page_size,$page_size)->getField("id,name,price_v1,price_v2,price_v3,info,preview,qrcode,shop_model_id");

		$resp = new \stdClass();
		if($models===null){
			$models = Array();
		}
		$resp->data=array_values($models);
		$resp->total=(int)$total;
		$this->ajaxReturn(pre_resp($resp));
		
	}
	public function buy_model(){
		$model_id = I("model_id");
		
		$MODINSP = M("PageModelsInshop");
		$mowh["sid"] = session("SHOP_ID");
		$mowh["page_model_id"] = $model_id;
		$mods = current($MODINSP->where($mowh)->getField("id,dead_line,status"));
		if(null!==$mods || strtotime($mods["dead_line"])>time()){
			$resp = new \stdClass();
			$resp->code = 200;
			$resp->msg = "您已经购买过这个模板，并且没有超过有效期。";
			$this->ajaxReturn(pre_resp($resp));
			die();
		}
			
		$PG = M("PageModels");
		$where["id"]=$model_id;
		$where["status"]="normal";
		$model = current($PG->where($where)->getField("id,price_v1,price_v2,price_v3,item_code_v1,item_code_v2,item_code_v3"));
		$sku_price = 0;
		$sku_item_code = "";
		switch(session("item_code")){
			case "FW_GOODS-1840332-1":
				$sku_price = $model["price_v1"];
				$sku_item_code = $model["item_code_v1"];
				break;
			case "FW_GOODS-1840332-v2":
				$sku_price = $model["price_v2"];
				$sku_item_code = $model["item_code_v2"];
				break;
			case "FW_GOODS-1840332-v3":
			case "FW_GOODS-1840332-v4":
				$sku_price = $model["price_v3"];
				$sku_item_code = $model["item_code_v3"];
				break;
		}
		if($sku_price>0){
			// 收费版获取付费链接
			
			$ORD = M("PageIsvOrders");
			$data["sid"] = session("SHOP_ID");
			$data["creat_date"] = date("Y-m-d H:i:s");
			$data["payment"] = $sku_price;
			$data["good_id"] = $model["id"];
			$data["good_type"] = "page_model";
			$data["status"] = "waiting_pay";
			$oid = $ORD->add($data);
			
			Vendor('taobaosdk/top/request/FuwuPurchaseOrderConfirmRequest');
			$req = new \FuwuPurchaseOrderConfirmRequest;
			$param_order_confirm_query_d_t_o = new \stdClass();
			$param_order_confirm_query_d_t_o->app_key=C("APPKEY");
			$param_order_confirm_query_d_t_o->item_code=$sku_item_code;
			$param_order_confirm_query_d_t_o->cyc_unit="3";
			$param_order_confirm_query_d_t_o->cyc_num="1";
			$param_order_confirm_query_d_t_o->out_trade_code="page_model_"+$oid;
			$param_order_confirm_query_d_t_o->device_type="pc";
			$param_order_confirm_query_d_t_o->quantity="1";
			$req->setParamOrderConfirmQueryDTO(json_encode($param_order_confirm_query_d_t_o));
			$resp = $this->c->execute($req);
			$resp->code=930;
			$resp->oid=$oid;
			$resp->msg="请您打开链接订购模板";
			$this->ajaxReturn(pre_resp($resp));
		}else{
			//免费版直接开通
			$ORD = M("PageIsvOrders");
			$data["sid"] = session("SHOP_ID");
			$data["creat_date"] = date("Y-m-d H:i:s");
			$data["payment"] = $sku_price;
			$data["good_id"] = $model["id"];
			$data["good_type"] = "page_model";
			$data["status"] = "sent_to_user";
			$ORD->add($data);
			
			$MODINSP = M("PageModelsInshop");
			if($mods!==null){
				// 到期模板续期
				$re = $MODINSP->where(Array("id"=>$mods["id"]))->save(Array("dead_line"=>date("Y-m-d H:i:s",time()+86400*365*20)));;
				if($re === false){
					$msg = "模板续费失败";
				}else{
					$msg = "模板续费成功";
				}
			}else{
				//新开通
				$modin["sid"] = session("SHOP_ID");
				$modin["page_model_id"] = $model["id"];
				$modin["creat_date"] = date("Y-m-d H:i:s");
				$modin["dead_line"] = date("Y-m-d H:i:s",time()+86400*365*20); //有效期20年
				$modin["status"] = "normal";
				$re = $MODINSP->add($modin);
				if($re === false){
					$msg = "模板开通失败";
				}else{
					$msg = "模板开通成功";
				}
			}
			$resp = new \stdClass();			
			$resp->new_mod_id = $re;
			$resp->msg = $msg;
			$this->ajaxReturn(pre_resp($resp));
		}
		

	}
	public function check_buy_model(){
		$model_id = I("model_id");
		$oid = I("oid");
		
		$PG = M("PageModels");
		$where["id"]=$model_id;
		$where["status"]="normal";
		$model = current($PG->where($where)->getField("id,price_v1,price_v2,price_v3,item_code_v1,item_code_v2,item_code_v3"));
		
		$is_buy=false;
		$sku_price = 0;
		$sku_item_code = "";
		switch(session("item_code")){
			case "FW_GOODS-1840332-1":
				$sku_price = $model["price_v1"];
				$sku_item_code = $model["item_code_v1"];
				break;
			case "FW_GOODS-1840332-v2":
				$sku_price = $model["price_v2"];
				$sku_item_code = $model["item_code_v2"];
				break;
			case "FW_GOODS-1840332-v3":
			case "FW_GOODS-1840332-v4":
				$sku_price = $model["price_v3"];
				$sku_item_code = $model["item_code_v3"];
				break;
		}

		Vendor('taobaosdk/top/request/VasOrderSearchRequest');
		$req = new \VasOrderSearchRequest;
		$req->setArticleCode(C("MOB_ITEM_CODE"));
		$req->setItemCode($sku_item_code);
		$req->setNick(session("VISITOR_NICK"));
		// $req->setStartCreated("2000-01-01 00:00:00");
		// $req->setEndCreated("2000-01-01 00:00:00");
		// $req->setBizType("1");
		// $req->setBizOrderId("12345");
		// $req->setOrderId("12345");
		// $req->setPageSize("20");
		// $req->setPageNo("1");
		$resp = $this->c->execute($req);
		if($resp->total_item>0){
			$is_buy=true;
			if($resp->total_item>1){
				$isv_order = $resp->article_biz_orders->article_biz_order[0];
			}else{
				$isv_order = $resp->article_biz_orders->article_biz_order;
			}
		}
		if($is_buy){
			
			//确认单提交后开通模板
			Vendor('taobaosdk/top/request/FuwuSpConfirmApplyRequest');
			$req = new \FuwuSpConfirmApplyRequest;
			$param_income_confirm_d_t_o = new \stdClass();
			$param_income_confirm_d_t_o->fee=$isv_order->fee->__toString();
			$param_income_confirm_d_t_o->nick=session("VISITOR_NICK");
			$param_income_confirm_d_t_o->extend='{"item_code":"'.session("item_code").'","good_type":"page_model","models_inshop_id":"'.$new_mod_id?$new_mod_id:$mods["id"].'"}';
			$param_income_confirm_d_t_o->appkey=C("APPKEY");
			$param_income_confirm_d_t_o->order_id=$isv_order->order_id->__toString();
			$param_income_confirm_d_t_o->out_order_id=$oid;
			$param_income_confirm_d_t_o->out_confirm_id=$oid;
			$req->setParamIncomeConfirmDTO(json_encode($param_income_confirm_d_t_o));
			$bill_resp = $this->c->execute($req, session("TOP_SESSION"));

			if($bill_resp && !$bill_resp->code){
				$confirm_id = $bill_resp->apply_result->__toString();
			}
			// else
			// {
				// $msg = '确认订单提交失败！';
				// $resp->code = 931;
				// $bill_resp = json_encode($bill_resp);
			// }
			
			$ORD = M("PageIsvOrders");
			$ORD->where(Array("id"=>$oid))
				->save(Array(
					"status"=>"sent_to_user",
					"order_id"=>$isv_order->order_id->__toString(),
					"confirm_id"=>$confirm_id,
					"confirm_fee"=>$isv_order->fee,
					"confirm_status"=>"waiting_confirm",
					));
					
			$MODINSP = M("PageModelsInshop");
			$mowh["sid"] = session("SHOP_ID");
			$mowh["page_model_id"] = $model_id;
			$mods = $MODINSP->where($mowh)->getField("id,dead_line,status");
			if($mods!==null){
				// 到期模板续期
				$mods = current($mods);
				$re = $MODINSP->where(Array("id"=>$mods["id"]))->save(Array("dead_line"=>date("Y-m-d H:i:s",time()+86400*365*20)));;
				if($re === false){
					$msg = "模板续费失败";
				}else{
					$msg = "模板续费成功";
				}
			}else{
				//新开通
				$modin["sid"] = session("SHOP_ID");
				$modin["page_model_id"] = $model["id"];
				$modin["creat_date"] = date("Y-m-d H:i:s");
				$modin["dead_line"] = date("Y-m-d H:i:s",time()+86400*365*20); //有效期20年
				$modin["status"] = "normal";
				$new_mod_id = $MODINSP->add($modin);
				if($new_mod_id === false){
					$msg = "模板开通失败";
				}else{
					$msg = "模板开通成功";
				}
			}
		}else{
			$msg = "您没有订购成功！请重试";
		}
		$resp->model_id=$model_id;
		$resp->is_buy=$is_buy?"true":"false";
		$resp->msg=$msg;
		$resp->bill_resp=$bill_resp;
		$this->ajaxReturn(pre_resp($resp));
		
	}
}